package org.hnxxxy.ebms.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.hnxxxy.ebms.common.Result;
import org.hnxxxy.ebms.listener.EasyExcelListener;
import org.hnxxxy.ebms.pojo.Plan;
import org.hnxxxy.ebms.pojo.Student;
import org.hnxxxy.ebms.service.StudentService;
import org.hnxxxy.ebms.vo.StudentResponseVo;
import org.hnxxxy.ebms.vo.TeaStudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WANGWEI
 * @since 2021-06-15
 */
@RestController
@RequestMapping("//student")
public class StudentController {
    @Autowired
    public StudentService studentService;

    @PostMapping("/add")
    @ApiOperation(value = "增加学生名单")
    public Result<String> addPlan(@RequestBody Student student){
        return studentService.add(student);
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "删除学生名单")
    public Result<String> delStudent(@PathVariable("id") Integer id){
        if (studentService.removeById(id)){
            return Result.success(200,"删除成功");
        }
        return Result.fail("删除失败");
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改学生名单")
    public Result<String> updatePlan(@RequestBody Student student){
        if (studentService.updateById(student)){
            return Result.success(200,"修改成功");
        }
        return Result.fail("修改失败");
    }

    @GetMapping("/get/{page}")
    @ApiOperation(value = "查询学生名单")
    public Result<IPage<Student>> getStudent(@PathVariable("page") int page){
        return studentService.getStudents(page);
    }

    @GetMapping("/tea/get/{page}")
    @ApiOperation(value = "查询地区所有招生学生名单")
    public Result<IPage<StudentResponseVo>> getTeaStudent(@PathVariable("page") int page){
        return studentService.getTeaTudent(page);
    }

    @PostMapping("/batchAdd")
    @ApiOperation(value = "导入收集到的学生信息")
    public Result batchStu(MultipartFile file){
        try{
            EasyExcel.read(file.getInputStream(),Student.class,new EasyExcelListener(studentService)).sheet().doRead();
        }catch (Exception e){
            System.out.println("导入失败");
        }
        return Result.success(200,"导入成功");
    }

    @GetMapping("/down")
    public void down(HttpServletResponse response){
        List<StudentResponseVo> hrStus = studentService.getStu();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = null;
        try {
            fileName = URLEncoder.encode("导出学生信息", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), StudentResponseVo.class).sheet("学生信息").doWrite(hrStus);
        } catch (Exception e) {
            System.out.println("导出错误");
        }
    }

}
